package com.core.email;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
@ConfigurationProperties(prefix = "spring.mail")
public class EmailConfig {
    private static String host;
    private static Integer port;
    private static String username;
    private static String password;
    private static boolean enable;
    private static int expireTime;

    private static int interval;

    @Bean
    public MailSender javaMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setPort(port);
        javaMailSender.setUsername(username);
        javaMailSender.setPassword(password);
        return javaMailSender;
    }
    @Value("${spring.mail.enable:false}")
    public void setFlag(boolean enable) {
        EmailConfig.enable = enable;
    }
    /**邮箱验证码有效期（分钟）*/
    @Value("${spring.mail.expireTime:5}")
    public void setExpireTime(int expireTime_) {
        expireTime = expireTime_;
    }
    /**邮件smtp服务地址*/
    @Value("${spring.mail.host:smtp.163.com}")
    public void setHost(String host_) {
        host = host_;
    }
    /**服务端口*/
    @Value("${spring.mail.port:25}")
    public void setPort(Integer port_) {
        port = port_;
    }
    /**服务用户名*/
    @Value("${spring.mail.username:zhongrui_email@163.com}")
    public void setUsername(String username_) {
        username = username_;
    }
    /**服务密码*/
    @Value("${spring.mail.password:YSNPEXUCXWFYMMJQ}")
    public void setPassword(String password_) {
        password = password_;
    }
    /**邮箱验证码发送间隔*/
    @Value("${spring.mail.interval:30}")
    public void setInterval(int interval_) {
        interval = interval_;
    }

    public static String getHost() {
        return host;
    }
    public static Integer getPort() {
        return port;
    }
    public static String getUsername() {
        return username;
    }
    public static String getPassword() {
        return password;
    }
    public static boolean isEnable() {
        return enable;
    }
    public static int getExpireTime() {
        return expireTime;
    }
    public static int getInterval() {
        return interval;
    }
}